package com.huike.clues.service.impl;

import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.clues.domain.TbActivity;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.AssignmentVo;
import com.huike.clues.domain.vo.FalseClueVo;
import com.huike.clues.mapper.*;
import com.huike.clues.service.ITbClueService;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.huike.clues.domain.TbClue;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.ITbClueService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class TbClueServiceImpl implements ITbClueService {
    @Resource
    TbClueMapper tbClueMapper;
    @Resource
    TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(TbClue tbClue) {
        //字段填充
        Date date = DateUtils.getNowDate();
        String name = SecurityUtils.getUsername();
        tbClue.setCreateBy(name);
        tbClue.setCreateTime(date);
        tbClue.setUpdateBy(name);
        tbClue.setUpdateTime(date);
        if (tbClue.getNextTime() == null) {
            return;
        }
        //插入线索表
        tbClueMapper.insert(tbClue);
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        tbClueTrackRecord.setClueId(tbClue.getId());
        tbClueTrackRecord.setCreateBy(tbClue.getOwner());
        tbClueTrackRecord.setSubject(tbClue.getSubject());
        tbClueTrackRecord.setLevel(tbClue.getLevel());
        tbClueTrackRecord.setCreateTime(tbClue.getCreateTime());
        tbClueTrackRecord.setNextTime(tbClue.getNextTime());
        //线索更进表
        tbClueTrackRecordMapper.insert(tbClueTrackRecord);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbClue tbClue) {
        //字段填充
        Date date = DateUtils.getNowDate();
        String name = SecurityUtils.getUsername();
        tbClue.setUpdateBy(name);
        tbClue.setUpdateTime(date);
        if (tbClue.getNextTime() == null) {
            return;
        }
        //删除
        //插入线索表
        tbClueMapper.update(tbClue);
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        tbClueTrackRecord.setClueId(tbClue.getId());
        tbClueTrackRecord.setCreateBy(tbClue.getOwner());
        tbClueTrackRecord.setSubject(tbClue.getSubject());
        tbClueTrackRecord.setLevel(tbClue.getLevel());
        tbClueTrackRecord.setCreateTime(tbClue.getCreateTime());
        tbClueTrackRecord.setNextTime(tbClue.getNextTime());
        //线索更进表
        tbClueTrackRecordMapper.update(tbClueTrackRecord);
    }




    @Resource
    private SysUserMapper userMapper;
    @Resource
    private TbClueMapper clueMapper;

    @Resource
    private TbClueTrackRecordMapper clueTrackRecordMapper;

    @Resource
    private TbAssignRecordMapper assignRecordMapper;

    @Resource
    private TbActivityMapper activityMapper;

    /**
     * 伪线索
     *
     * @param falseClueVo
     * @param id
     */
    @Override
    @Transactional
    public void falseClue(FalseClueVo falseClueVo, Long id) {
        TbClue clue = clueMapper.getById1(id);
        //如果被标记伪线索次数超过3次,直接删除此线索
        if (clue.getFalseCount() + 1 > 3) {
            clueMapper.deleteById(id);
            return;
        }

        clue.setStatus(TbClue.StatusType.FALSE.getValue());
        clueMapper.updateByCondition(clue);
        TbClueTrackRecord clueTrackRecord = new TbClueTrackRecord();
        BeanUtils.copyProperties(clue, clueTrackRecord);
        clueTrackRecord.setClueId(id);
        clueTrackRecord.setFalseReason(falseClueVo.getReason());
        clueTrackRecord.setRecord(falseClueVo.getRemark());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    /**
     * 批量捞取线索
     *
     * @param assignmentVo
     */
    @Override
    public void gain(AssignmentVo assignmentVo) {
        //获取assignmentVo里面的信息
        Long[] clueIds = assignmentVo.getIds();
        Long userId = assignmentVo.getUserId();

        //根据userId获取user信息
        SysUser user = userMapper.selectUserById(userId);

        //创建tbAssignRecord对象,补全数据,未插入新的分配记录表做准备
        TbAssignRecord tbAssignRecord = new TbAssignRecord();
        tbAssignRecord.setUserId(userId);
        tbAssignRecord.setUserName(user.getUserName());
        tbAssignRecord.setDeptId(user.getDeptId());
        tbAssignRecord.setCreateBy(SecurityUtils.getUsername());
        Date date = DateUtils.getNowDate();
        tbAssignRecord.setCreateTime(date);

        for (Long clueId : clueIds) {
            //把线索的状态改成正常的
            clueMapper.updateStatus(clueId, TbClue.StatusType.UNFOLLOWED.getValue());
            //补全tbAssignRecord数据
            tbAssignRecord.setAssignId(clueId);
            //新插入一条分配记录表
            assignRecordMapper.insert(tbAssignRecord);
        }
    }

    /**
     * 查询线索管理列表
     *
     * @param clue
     * @return
     */
    @Override
    public List<TbClue> selectClueList(TbClue clue) {
        List<TbClue> clueList = clueMapper.selectClueList(clue);
        return clueList;
    }

    /**
     * 查询线索池
     *
     * @param clue
     * @return
     */
    @Override
    public List<TbClue> selectCluePool(TbClue clue) {
        List<TbClue> clueList = clueMapper.selectCluePoll(clue);
        for (TbClue tbClue : clueList) {
            if (tbClue.getActivityId() != null) {
                TbActivity tbActivity = activityMapper.getById(tbClue.getActivityId());
                if (tbActivity != null) {
                    tbClue.setActivityName(tbActivity.getName());
                }
            }
        }
        return clueList;
    }


    /**
     * 根据id获取线索基础信息
     * @param id
     * @return TbCule
     */
    @Override
    public TbClue findClueById(Long id) {
        return tbClueMapper.findClueById(id);
    }
}
